public class Test12 {
    public static Animal func3(){
        return new Bird("小海",7,"男");
    }
    public static void func1(Animal animal){

    }
    public static void main(String[] args) {
        //向下转型 不安全
        Animal animal3 = new Bird("鸟",8,"女");
        if(animal3 instanceof Bird) {
            Bird bird = (Bird) animal3;
            bird.fly();
        }else {
            System.out.println("不能飞");
        }
            //向上转型：父类引用 引用了子类对象
        //只能访问Animal这个类中的方法
        //1、直接赋值 -》 向上转型
    Animal animal = new Dog("小黄",5,"男");
    animal.eat();
    animal.eat(2);
    Animal animal1 = new Bird("小紫",6,"男");
    //2、方法传参 -》 向上转型(给一个父类类型的参数来接受子类的引用)
    Dog dog = new Dog("小黑",6,"女");
    func1(dog);
    Bird bird = new Bird("小海",7,"男");
    func1(bird);
    //3、返回值 -》向上转型
    Animal animal21 = func3();
    }
}
class Animal{
    String name;
    int age;
    String sex;
    public void eat(){
        System.out.println(this.name + "会吃饭");
    }

    public Animal(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    //方法的重载，静态绑定：在编译时就确定所用的方法是它
    public void eat(int a){
        System.out.println("eat eat eat");
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}
class Dog extends Animal{
    public Dog(String name, int age, String sex) {
        super(name, age, sex);
    }
//    public void eat(int a){
//        System.out.println("eat eat eat");
//    }

        public void bark(){
        System.out.println(this.name + "汪汪汪");
    }
    //动态绑定：方法的重写，程序在运行时才确定所用的方法是它
    public void eat(){
        System.out.println(this.name + "正在吃狗粮");
    }
}
class Bird extends Animal{
    public Bird(String name, int age, String sex) {
        super(name, age, sex);
    }
    public void fly(){
        System.out.println("正在飞");
    }
}